import 'package:external_app_launcher/external_app_launcher.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:my_app/utils/index.dart';


class OpenAppTestPage extends StatefulWidget {
  const OpenAppTestPage({super.key});

  @override
  State<OpenAppTestPage> createState() => _OpenAppTestPageState();
}

class _OpenAppTestPageState extends State<OpenAppTestPage> {

  final appList = {
    "taobao": "淘宝",
    "qq": "QQ",
    "weixin": "微信",
    "baidu": "京东",
    "meituan": "美团",
    "dianping": "点评",
    "zfb": "支付宝",
    "weibo":  "微博",
    "zhihu": "知乎",
    "douban": "豆瓣fm",
    "uc": "uc浏览器",
    "baiduMap": "百度地图",
    "youku": "优酷",
    "youdaoDict": "有道词典",
    "douyin": "抖音短视频",
    "toutiao": "今日头条",
    "toutiaoLite": "今日头条极速版",
    "pdd": "拼多多",
    "boss": "BOSS直聘",
    "aqy": "爱奇艺",
    "dingding": "钉钉",
    "xhs": "小红书",
  };

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Launch APP Test")),
      body: SizedBox(
        height: double.infinity,
        child: ListView.builder(
          shrinkWrap: true,
          itemCount: appList.length,
          itemBuilder: (_, int index) {
            String key = appList.keys.elementAt(index);
            String value = appList[key]!;
            AppInfo item = AppSchema.toMap()[key]!;
          
            return ElevatedButton(onPressed: () async {
              bool isInstalled = await LaunchApp.isAppInstalled(
                androidPackageName:item.androidPackageName,
                iosUrlScheme: item.iosUrlScheme
              );
              if (isInstalled) {
                int openResult = await LaunchApp.openApp(
                  androidPackageName:item.androidPackageName,
                  iosUrlScheme: item.iosUrlScheme
                );
                if (openResult == 0) {
                  Fluttertoast.showToast(msg: "$value打开失败");
                }
              } else {
                Fluttertoast.showToast(msg: "$value没有安装");
              }
            }, child: Text("打开$value"));
          }
        ),
      )
    );
  }
}
